package com.bjpowernode.web;

import com.bjpowernode.entity.TCustomerRemark;
import com.bjpowernode.query.TCustomerRemarkQuery;
import com.bjpowernode.result.R;
import com.bjpowernode.service.TCustomerRemarkService;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 客户备注信息表
 */
@RestController
@Slf4j
@RequestMapping("/api/customerRemark")
public class TCustomerRemarkController {

    @Resource
    private TCustomerRemarkService tCustomerRemarkService;

    @PostMapping("save")
    public R saveRemark(@Validated @RequestBody TCustomerRemarkQuery tCustomerRemarkQuery,
                        @RequestHeader(value = "Authorization") String jwt) {
        log.info("要添加的客户备注 : {}", tCustomerRemarkQuery);
        tCustomerRemarkQuery.setJwt(jwt);
        int save = tCustomerRemarkService.saveRemark(tCustomerRemarkQuery);
        return save >= 1 ? R.OK() : R.FAIL();
    }

    @GetMapping("page")
    public R loadCustomerHistoryRemark(@RequestParam(value = "customerId") Integer customerId,
                                       @RequestParam(value = "current") Integer current) {
        log.info("加载历史备注信息对应的id ,以及当前页 : {} , {}", customerId, current);
        PageInfo<TCustomerRemark> pageInfo = tCustomerRemarkService.loadCustomerHistoryRemark(customerId , current);
        return R.OK(pageInfo);
    }

    @DeleteMapping("remove/{id}")
    public R removeRemarkById(@PathVariable(value = "id")Integer id) {
        log.info("要删除的备注信息id : {}" , id);
        int update = tCustomerRemarkService.removeById(id);
        return update >= 1 ? R.OK() : R.FAIL();
    }

    @GetMapping("load/{id}")
    public R loadCustomerEditRemark(@PathVariable(value = "id")Integer id) {
        log.info("客户备注信息回调id : {}" , id);
        TCustomerRemark tCustomerRemark = tCustomerRemarkService.loadCustomerRemark(id);
        return R.OK(tCustomerRemark);
    }

    @PutMapping("update")
    public R editRemarkDialogInfo(@RequestBody TCustomerRemarkQuery tCustomerRemarkQuery,
                                  @RequestHeader(value = "Authorization")String jwt) {
        log.info("要更新的备注信息: {}" , tCustomerRemarkQuery);
        tCustomerRemarkQuery.setJwt(jwt);
        int update = tCustomerRemarkService.updateRemarkById(tCustomerRemarkQuery);
        return update >= 1 ? R.OK() : R.FAIL();
    }
}
